﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Module.CarVenues
{
    public class CarUserInfo : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string key = "VenuesId";
            string venuesIdString = context.Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                int venuesId = int.Parse(venuesIdString);
                BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                Model.Venues_Main venues = BLLVenues.GetModel(venuesId);
                string[] strCId = venues.RaceClub.Split(',');
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat("ID = {0} ", strCId[0]);
                if (strCId.Length > 1)
                {
                    for (int i = 1; i < strCId.Length; i++)
                    {
                        strWhere.AppendFormat("OR ID = {0} ", strCId[i]);
                    }
                }
                List<Model.Club_Main> listClub = BLLClub.GetModelList(strWhere.ToString());
                BLL.User.Friend BLLFriend = new Jy123.BLL.User.Friend();
                string strUserWhere = string.Format("ID IN (SELECT FFriend FROM User_Friend WHERE FUser = {0})", venues.FUser.Value.ToString());
                List<Model.User_Main> listUser = BLLUser.GetModelList(strUserWhere);
                Dictionary<int, Model.User_Main> dicUser = new Dictionary<int, Jy123.Model.User_Main>();
                foreach (Model.User_Main user in listUser)
                {
                    dicUser.Add(user.ID, user);
                }
                string cache=context.Cache["carraceli"].ToString();
                StringBuilder result=new StringBuilder();
                for (int j = 0; j < listClub.Count; j++)
                {
                    string cCache = cache;
                    Model.Club_Main cClub=listClub[j];
                    Model.User_Main cUser=dicUser[cClub.FUser.Value];
                    cCache = cCache.Replace("$AId$","a"+j.ToString());
                    cCache = cCache.Replace("$UserName$",cUser.UserName);
                    cCache = cCache.Replace("$HiddenId$","hidden"+j.ToString());
                    cCache = cCache.Replace("$Value$",j.ToString());
                    result.Append(cCache);
                }
                context.Response.Write(result);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
